import {body, description, query, request, summary, tagsAll} from "koa-swagger-decorator-fork";
import {parseFilesFromMetaXml} from "../../utils/nc";
import config from "config";
import {FileFavoriteSchema} from "../../schema";
const publicPath = config.get('nextcloud.share_path')

@tagsAll(['FavoriteFile'])
export default class FavoriteFileRouter {
    @request('GET', '/favorites')
    @summary('get file activities by file id')
    @query({
        path: { type: 'string'}
    })
    @description('查询收藏文件')
    static async getFavoriteFile(ctx) {
        let files,path = ctx.query.path||'/',favorites,
            shareFolder = `/files/${ctx.user.id}${publicPath}`
        files = await ctx.client.customRequest(`${shareFolder}${path}`, {
            method: "REPORT",
            headers: {
                Accept: "text/xml",
                Depth: "infinity",
                "Content-Type": "application/xml; charset=utf-8"
            },
            data: `<?xml version="1.0"?>    
                      <oc:filter-files  xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns" xmlns:nc="http://nextcloud.org/ns">
                             <oc:filter-rules>
                                     <oc:favorite>1</oc:favorite>
                             </oc:filter-rules>
                             <d:prop>
                                <oc:fileid />
                                <oc:tags />
                                <d:getlastmodified />
                                <oc:size />
                                <d:getcontenttype />
                                <d:displayname/>
                                <nc:mount-type />
                                <oc:owner-id />
                              </d:prop>
                     </oc:filter-files>`,
            responseType: "text"
        });
        favorites = parseFilesFromMetaXml(files,`/remote.php/dav/files/${ctx.user.id}${publicPath}`)
        ctx.body = favorites
    }

    @request('POST', '/favorites')
    @summary('add favorites')
    @description('收藏文件')
    @body(FileFavoriteSchema)
    static async setFavoriteFlag(ctx) {
        let {name,path,favorite} = ctx.request.body,filePath;
        path = path||'/'
        filePath = `/files/${ctx.user.id}${publicPath}${path}/${name}`
        favorite = favorite?1:0
        await ctx.client.customRequest(filePath, {
            method: "PROPPATCH",
            headers: {
                Accept: "text/xml",
                Depth: "infinity",
                "Content-Type": "application/xml; charset=utf-8"
            },
            data: `<?xml version="1.0"?>
                    <d:propertyupdate xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns">
                      <d:set>
                            <d:prop>
                              <oc:favorite>${favorite}</oc:favorite>
                            </d:prop>
                      </d:set>
                    </d:propertyupdate>`,
            responseType: "text"
        });
        ctx.body = {}
    }
}
